package com.weiyi.mashangzhu.Roomer.Activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangePwdActivity extends BaseActivity {

    @BindView(R.id.change_pwd_1)
    EditText changePwd1;
    @BindView(R.id.change_pwd_2)
    EditText changePwd2;
    @BindView(R.id.change_pwd_3)
    EditText changePwd3;
    String info,url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        ButterKnife.bind(this);
        info=getIntent().getStringExtra("info");
        Log.e("chhhhhhhhhhh","="+info);
        if (info.equals("host")){
            //房东端修改密码
            url=API.BaseApi_L;
        }else {
            //房客端修改密码
            url=API.BaseApi_R;
        }
    }


    @OnClick({R.id.back, R.id.change_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.change_confirm:
                if ((changePwd1.getText() + "").equals("")) {
                    T.showShort(this, "请输入原密码");
                } else {
                    if ((changePwd2.getText() + "").equals("")) {
                        T.showShort(this, "请输入新密码");
                    } else {
                        if ((changePwd2.getText() + "").trim().length() > 16 || (changePwd2.getText() + "").trim().length() < 6) {
                            T.showShort(this, "请输入6-16位新密码");
                        } else {
                            if ((changePwd3.getText() + "").equals("")) {
                                T.showShort(this, "请确认新密码");
                            } else {
                                LoadingDialog.show(ChangePwdActivity.this);
                                OkHttpUtils.get()
                                        .addParams("token", SPUtils.get(this, "token", "") + "")
                                        .addParams("beforePassword", changePwd1.getText() + "")
                                        .addParams("newPassword", changePwd2.getText() + "")
                                        .addParams("surePassword", changePwd3.getText() + "")
                                        .url(url + API.ChangePssword)
                                        .build()
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onError(Request request, Exception e) {
                                                LoadingDialog.dismiss(ChangePwdActivity.this);
                                                T.showShort(ChangePwdActivity.this, "数据获取失败，请稍后再试");

                                            }

                                            @Override
                                            public void onResponse(String response) {
                                                LoadingDialog.dismiss(ChangePwdActivity.this);
                                                Log.e("chhhhhhhh", "=" + response);
                                                if (response != null) {
                                                    try {
                                                        JSONObject jsonObject = new JSONObject(response);
                                                        if (jsonObject.getInt("status") == 0) {
                                                            finish();
                                                        }
                                                        T.showShort(ChangePwdActivity.this, jsonObject.getString("msg"));
                                                    } catch (JSONException e) {
                                                        e.printStackTrace();
                                                    }
                                                }

                                            }
                                        });
                            }
                        }
                    }
                }
                break;
        }
    }

}
